package juc.semaphore;

import java.util.concurrent.Exchanger;

/*jdk21虚拟线程*/
public class ExchangerDemo {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();
        String money = "500 rmb";
        String goods = "phone";
        new Thread(() -> {
            System.out.println("goods is ready");
            try {
                String exchange = exchanger.exchange(goods);
                System.out.println("saler:"+exchange);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        new Thread(() -> {
            System.out.println("money is ready");
            try {
                String exchange = exchanger.exchange(money);
                System.out.println("buyer:" + exchange);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

    }
}